<?php

/*
	* OpenGSP (c) 2011
	* Author(s): Christopher Martino
	* Module: products.base.php
	* Updated: 15/04/2011
	* http://opengsp.com/
*/

class Products_BASE
{
	function is_valid_product($product_id)
	{
		return MySQLi_BASE::total_rows("SELECT * FROM OpenGSP_Products WHERE Product_ID = '".MySQLi_BASE::escape_input($product_id)."'") == 1 ? true:false;
	}

	public static function create_product($product_name, $product_slots, $product_port, $product_archive, $product_pps)
	{
		if(strlen($product_name) < 5) return INVALID_PRODUCT_STRLEN;
		if(strlen($product_archive) < 5) return INVALID_ARCHIVE_STRLEN;
		if(!is_numeric($product_slots)) return INVALID_SLOT_FORMAT;
		if(!is_numeric($product_port)) return INVALID_PORT_FORMAT;
		if(!gettype($product_pps) != "integer") return INVALID_PRICE_FORMAT;
		
		return MySQLi_BASE::query_database("INSERT INTO OpenGSP_Products (Product_Name, Product_Slots, Product_Archive, Product_DefPort, Product_PPSlot) VALUES('".MySQLi_BASE::escape_input($product_name)."', '".MySQLi_BASE::escape_input($product_slots)."', '".MySQLi_BASE::escape_input($product_archive)."'. '".MySQLi_BASE::escape_input($product_port)."', '".MySQLi_BASE::escape_input($product_pps)."')");
	}

	public static function delete_product($product_id)
	{
		if(!$this->is_valid_product($product_id)) return 0;
		
		return MySQLi_BASE::query_database("DELETE FROM OpenGSP_Products WHERE Product_ID = '".MySQLi_BASE::escape_input($product_id)."'");
	}
	
	public static function product_information($product_id)
	{
		if(!$this->is_valid_product($product_id)) return 0;
		
		$this_product = MySQLi_BASE::data_array("SELECT * FROM OpenGSP_Products WHERE Product_ID = '".MySQLi_BASE::escape_input($product_id)."'");
		
		return array('ProductName' => $this_product['Product_Name'], 'ProductSlots' => $this_product['Product_Slots'], 'ProductPort' => $this_product['Product_DefPort'], 'Product_Price' => $this_product['Product_PPSlot']);
	}
	
	public static function process_order($account_id, $product_id, $product_slots, $product_location)
	{
		if(!$this->is_valid_product($product_id)) return 0;
		
		$this_product = $this->product_information($product_id);
		
		$order_price = $this_product['Product_Price'] * (int)$product_slots;
		
		$class_base['Accounts']->update_account_balance($account_id, "-".$order_price);
	}
}